<?php


namespace api\zmt\controller;


use api\zmt\model\UserModel;
use api\zmt\validate\UserValidate;
use cmf\controller\RestBaseController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\Exception;
use think\facade\Request;
use think\Filesystem;


class UserController extends RestBaseController
{

    //验证码
    public function sendCode(){
        $mobile = input('mobile');
        //验证验证码
        $code = sendAliSms($mobile);
        if($code['code'] == 1){
            $this->success('发送成功');
        }else{
            $this->error($code['msg']);
        }
    }
    //注册
    public function register(){
        //接收数据
        $data = input('post.');
//        halt($data);
        //查重
        $mobile = UserModel::where('mobile',$data['mobile'])->find();
        if($mobile){
            $this->error('账号已存在');
        }
        //验证
        $validate = new UserValidate();
        $result = $validate->scene('register')->check($data);
        //判断
        if(!$result){
            $this->error($validate->getError());
        }
        //验证验证码是否存在
        // $code = Db::name('sms_code_log')->where('mobile',$data['mobile'])->find();
        $check = checkSmsCode($data['mobile'],$data['code']);
//        halt($check);
        if(!$check){
            $this->error('验证码错误');
        }
        ;
        $user = UserModel::create([
            'mobile' => $data['mobile'],
        ]);

        //入库
        if($user){
            $this->success('注册成功');
        }else{
            $this->error('注册失败');
        }
    }
    //登录
    public function login(){
        //接收
        $data = input('post.');
        //验证
        $validate = new UserValidate();
        $result = $validate->scene('login')->check($data);
        //判断
        if(!$result){
            $this->error($validate->getError());
        }
        //查询账号
        $user = UserModel::where('mobile',$data['mobile'])->find();
        //验证验证码是否存在
        $check = checkSmsCode($data['mobile'],$data['code']);
        if(!$check){
            $this->error('验证码错误');
        }
        if(!$user){
            $this->error('该账号不存在');
        }
        $this->success('登陆成功',[
            //JWT
            'token' => signToken($user['id'],$user['mobile']),
        ]);
    }
    //个人信息
    public function message()
    {
        $header = request()->header();
        if(!isset($header['token']) || empty($header['token'])) {
            $this->error('缺少token !');
        }
        $key = new Key(config('app.jwt_key'),'HS256');
        try {
            $res = JWT::decode($header['token'],$key);
        }catch (\Exception $exception) {
            $this->error($exception->getMessage());
        }
        //halt($res->data->id);
        $userInfo = UserModel::field('id,name,image,mobile')
            ->where('id',$res->data->user_id)
            ->find();
//        halt($userInfo);
        $userInfo['image'] = \request()->domain(). '/storage/' . $userInfo['image'];
        $this->success('用户信息',$userInfo);
    }


    //个人信息修改

    public function msgEditPost(){
        $header = request()->header();
        if(!isset($header['token']) || empty($header['token'])) {
            $this->error('缺少token !');
        }
        $key = new Key(config('app.jwt_key'),'HS256');
        try {
            $res = JWT::decode($header['token'],$key);
        }catch (\Exception $exception) {
            $this->error($exception->getMessage());
        }
        //halt($res->data->id);
//        $userInfo = UserModel::field('id,name,image,mobile')
//            ->where('id',$res->data->user_id)
//            ->find();
//        halt($userInfo);
//        $userInfo['image'] = \request()->domain(). '/storage/' . $userInfo['image'];
        $data = input('post.');
        $validate = new UserValidate();
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        //查询用户是否存在
        $user = UserModel::find($res->data->user_id);
        if(!$user){
            $this->error('用户不存在');
        }
        //修改
        $edit = UserModel::where('id',$data['id'])
            ->update([
                'image' => $data['image'],
                'name' => $data['name'],
                'mobile' => $data['mobile'],
                'create_time' => date('Y-m-d H:i:s')
            ]);
        //判断修改是否完成
        if($edit){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

}